#include <ani.h>
#include <array>
#include <iostream>
#include <vector>

// 这个类型不是ani_array
static void handleData(ani_env *env, ani_arraybuffer arraybuffer) // native函数
{
    void* resultData;
    ani_size resultSize;
    env->ArrayBuffer_GetInfo(arraybuffer, &resultData, & resultSize);
    // 注意解指针uint32_t*
    std::cout << *static_cast<uint32_t*>(resultData) << std::endl;
}

ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result)
{
    ani_env *env;
    if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) {
        std::cerr << "Unsupported ANI_VERSION_1" << std::endl;
        return ANI_ERROR;
    }

    static const char *moduleName = "Lani_arraybuffer;";
    ani_module module;
    if (ANI_OK != env->FindModule(moduleName, &module)) {
        std::cerr << "Not found '" << moduleName << "'" << std::endl;
        return ANI_ERROR;
    }

    std::array methods = {
        ani_native_function {"handleData", nullptr, reinterpret_cast<void *>(handleData)},
        
    };
    std::cout << "Start bind native methods to '" << moduleName << "'" << std::endl;

    if (ANI_OK != env->Module_BindNativeFunctions(module, methods.data(), methods.size())) {
        std::cerr << "Cannot bind native methods to '" << moduleName << "'" << std::endl;
        return ANI_ERROR;
    };
    std::cout << "Finish bind native methods to '" << moduleName << "'" << std::endl;

    *result = ANI_VERSION_1;
    return ANI_OK;
}